// pages/me/article/index.js
const app = getApp();
const common = require('../../utils/common.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {

        articleList: [],  //已发布文章列表
        page: 0,             //审核中文章页数标记

    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {



    },
    //获取文章列表公共函数
    getArticle: function (url, page = 0) {      //page:页数
        // var is_login = common.checkLogin();
        // if (!is_login) return;
        var that = this;
        var token = common.getToken();
        var userId = common.getUserId();
        var start = page++ * 10;
        var articleUserId=that.options.userId;
        console.log(articleUserId);
        var data = { token: token, userId: userId, start: start,articleUserId:articleUserId };

        var msg = '加载中';
        var func = function (content) {

            if (content.articleList && content.articleList.length > 0) {
                let data = {};
                data.detail=content;
                if (page > 1) {
                    data.articleList = that.data.articleList.concat(content.articleList);       //加载更多页数显示方法
                } else {
                    data.articleList = content.articleList;
                }

                data.page = page;
                that.setData(data);
            } else {
                if (page > 1) {
                    common.toast('没有更多了');
                }
            }
        };
        var res = common.request(url, data, msg, func, that);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //获取列表
        var url = `${app.apiHost}homepage/mainArticle`;  

        this.getArticle(url);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        //获取列表
        var url = `${app.apiHost}homepage/ajaxArticle`;  
        var page = this.data.page;
        this.getArticle(url, page);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    guanzhu:function(e){
        
        var is_login = common.checkLogin();
        if (!is_login) return;
       
        var followUserId=e.currentTarget.dataset.followid;  //被关注者ID
        var type=e.currentTarget.dataset.type;      //是否关注
        if(type==0){
            type=1;
        }else if(type==1){
            type=0;
        }
        var that = this;
        var token = common.getToken();
        var userId = common.getUserId();
        var url = `${app.apiHost}bbs/follow`;   //关注接口
        var data = { token: token, userId: userId, followUserId:followUserId,type:type};

        var msg = '';
        var func = function (content) {
                let data=that.data;
                data.detail.guanzhu=type;
                that.setData(data);
        };
        var res = common.request(url, data, msg, func, that);
    }

})